﻿/************************************************************************

 This file is part of "10 Second Tim".

 "10 Second Tim" is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 "10 Second Tim" is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with "10 Second Tim".  If not, see <http://www.gnu.org/licenses/>.

************************************************************************/

// All code (c)2013 Scott Bevin. all rights reserved

using LDE;
using LDE.Input;
using LudumDare27.Games;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace LudumDare27.GameScreens
{
	internal class MainMenuScreen : GameScreen
	{
		private Texture2D logoTexture;
		private Menu menu;

		private GameScreen gamescreenToTrigger = null;
		private WhiteFadeScreen whiteFade = null;

		public MainMenuScreen( )
			: base( true )
		{
		}

		public override void OnAdded( )
		{
			logoTexture = ContentManager.Load<Texture2D>( "Img/GameLogo" );
			menu = new Menu( ContentManager.Load<SpriteFont>( "Fonts/DebugFont" ), ContentManager.Load<SpriteFont>( "Fonts/DebugFont" ), ContentManager );

			menu.OnBack += Back;

			menu.AddEntry( "Play", this.NewGame );
#if !SILVERLIGHT
			menu.AddEntry( "Options", this.Options );
#endif

			//menu.AddEntry("How To Play / Credits", this.HelpMenu);
#if !SILVERLIGHT
			menu.AddEntry( "Quit", this.QuitGame );
#endif

			menu.AddNonClickEntry( " ", null );
			menu.AddNonClickEntry( "www.gameXcore.co.uk", null );
			menu.AddNonClickEntry( "scott@gameXcore.co.uk", null );
			menu.AddNonClickEntry( "Ludum Dare 27 - http://www.ludumdare.com/", null );

			base.OnAdded( );
		}

		public override void OnRemoved( )
		{
			base.OnRemoved( );
		}

		public override void Update( float dt )
		{
			menu.Update( dt, Player.Get.GetInput( this ), this.Game.GraphicsDevice );

			if ( whiteFade != null && whiteFade.State == WhiteFadeScreen.States.FadedIn )
			{
				whiteFade.FadeOut( );
				GameScreenManager.AddGameScreen( gamescreenToTrigger, whiteFade, GameScreenManager.AddGameScreenData.PositioningOptions.BelowRef );
				this.KillScreen = true;
			}

			base.Update( dt );
		}

		public override void Render( float dt, GraphicsDevice gd, SpriteBatch sprites )
		{
			Vector2 basePosition = new Vector2( ( (float)gd.PresentationParameters.BackBufferWidth ) * 0.5f,
				( (float)gd.PresentationParameters.BackBufferHeight ) * 0.25f );

			sprites.Begin( SpriteSortMode.Deferred, BlendState.NonPremultiplied );

			sprites.Draw( logoTexture, basePosition, null, Color.White, 0.0f,
				new Vector2( ( (float)logoTexture.Width ) * 0.5f, ( (float)logoTexture.Height ) * 0.5f ), Vector2.One, SpriteEffects.None, 0.0f );

			sprites.End( );

			menu.Render( gd, sprites );

			base.Render( dt, gd, sprites );
		}

		private void Back( Menu.MenuEntry entry )
		{
			whiteFade = new WhiteFadeScreen( );
			gamescreenToTrigger = new ChooseInputScreen( );
			GameScreenManager.AddGameScreen( whiteFade, null, GameScreenManager.AddGameScreenData.PositioningOptions.OnTop );
		}

		private void NewGame( Menu.MenuEntry entry )
		{
			whiteFade = new WhiteFadeScreen( );

			//gamescreenToTrigger = ( (LD27Game)Game ).ChooseNextGame( null, 1 );
			gamescreenToTrigger = new GameplayIntroScreen( false, 0 );

			//gamescreenToTrigger = new HelpScreen();

			GameScreenManager.AddGameScreen( whiteFade, null, GameScreenManager.AddGameScreenData.PositioningOptions.OnTop );
		}

#if !SILVERLIGHT

		private void Options( Menu.MenuEntry entry )
		{
			whiteFade = new WhiteFadeScreen( );
			gamescreenToTrigger = new OptionsMenuScreen( );

			//gamescreenToTrigger = new HelpScreen();

			GameScreenManager.AddGameScreen( whiteFade, null, GameScreenManager.AddGameScreenData.PositioningOptions.OnTop );
		}

#endif

		private void HelpMenu( Menu.MenuEntry entry )
		{
			whiteFade = new WhiteFadeScreen( );

			//gamescreenToTrigger = new HelpScreen( );

			GameScreenManager.AddGameScreen( whiteFade, null, GameScreenManager.AddGameScreenData.PositioningOptions.OnTop );
		}

		private void QuitGame( Menu.MenuEntry entry )
		{
			this.Game.Exit( );
		}
	}
}